<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Vue数据双向绑定</title>
</head>
<body>
  <div id="app">
    输入号码:<input type="text" v-model.lazy="phone"> 
    <input type="button" value="添加号码" :disabled="dis" @click="add">
    <h2>{{tips}}</h2>
    <h3>{{phones}}</h3>
  </div>
  <script src="vue.js"></script>
  <script>
    const vm  = new Vue({
      el:"#app",
      data:{
        addOk:false,
        dis:true,
        phone:'',
        tips:'',
        phones:[
          '13209870010',
          '13908700156',
          '11911911911',
          '11411411411',
          '11111111111',
          '22222222222'
        ]
      },
      watch:{
        phone(newVal,oldVal) {
          if(isNaN(newVal)) {
            this.tips = '请输入一个有效的数字'
            this.addOk = false
            this.dis = true
            return;
          }
          if(newVal=='') {
            this.tips = ''
            this.dis = true
            return;
          }
          if(newVal.length!=11) {
            this.tips = "号码的长度不正确!"
            this.addOk = false
            this.dis = true
            return;
          }
          this.tips = ''
          let n = this.phones.findIndex(e=>e==newVal)
          if(n==-1) {
            //没有找到
            this.addOk = true
            this.dis = false  
            this.tips = "这是一个有效的号码"
          } else {
            this.dis = true
            this.tips="该号码已经存在!不能添加"
          }
          
        }
      },
      methods: {
        add() {
          this.phones.push(this.phone)
          this.phone = ''
          this.tips = ''
        }
      },
    })
  </script>
</body>
</html>